<?php
require_once "./Application/Common/Common/arrayList.php";

//空值数组返回，一般用于判断
function notEmpty($arr){
foreach($arr as $key=>$value){
if(I($key)=="")
	return $value;
}
return false;
}

//生成二维码
function create_qrcode($value,$id,$logo=false){
	include './Application/Common/Common/phpqrcode.php'; 
	//$value = 'http://www.cnblogs.com/txw1958/'; //二维码内容   
	$errorCorrectionLevel = 'L';//容错级别   
	$matrixPointSize = 6;//生成图片大小   
	//生成二维码图片   
	\QRcode::png($value, './Public/Common/qrcode/'.$id.'.png', $errorCorrectionLevel, $matrixPointSize, 2);    
	$QR = './Public/Common/qrcode/'.$id.'.png';//已经生成的原始二维码图   

	if ($logo !== FALSE) {   
	    $QR = imagecreatefromstring(file_get_contents($QR));   
	    $logo = imagecreatefromstring(file_get_contents($logo));   
	    $QR_width = imagesx($QR);//二维码图片宽度   
	    $QR_height = imagesy($QR);//二维码图片高度   
	    $logo_width = imagesx($logo);//logo图片宽度   
	    $logo_height = imagesy($logo);//logo图片高度   
	    $logo_qr_width = $QR_width / 5;   
	    $scale = $logo_width/$logo_qr_width;   
	    $logo_qr_height = $logo_height/$scale;   
	    $from_width = ($QR_width - $logo_qr_width) / 2;   
	    //重新组合图片并调整大小   
	    imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,   
	    $logo_qr_height, $logo_width, $logo_height);   
		imagepng($QR,'./Public/Common/qrcode/'.$id.'_logo.png'); 
	}
}


function cardhidden($cardNumber){
   $char="x";
   $numToHide=8;
   $cardNumber = substr($cardNumber, 0, strlen($cardNumber) - $numToHide);        
   for($i = 0; $i < $numToHide; $i++){     
     $cardNumber .= $char;     
   }
  return $cardNumber;
}
	
//随机密码生成函数，参数1个，密码的位数
function makePass($length) { 
$possible = "0123456789". 
"abcdefghijklmnopqrstuvwxyz". 
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
$str = ""; 
while(strlen($str) <$length) 
{ 
$str .= substr($possible, (rand() % strlen($possible)), 1); 
} 
return($str); 
} 	

//通用查询方法2
//$mist_arr =>模糊查询的数组key=>value
//$accute_arr =>精确查询数据key=>value
//需要计入cookie的数组key
function searchCon($mist_arr=array(),$accute_arr=array(),$cookie_arr=array()){

//如果cookie_arr不为空
if($cookie_arr){
foreach($cookie_arr as $key=>$value){
if(cookie($value)!="")
	$con[$value]=cookie($value);
}
}

/*-----------以上是cookie的查询字段------------*/


//模糊数组
if($mist_arr){

foreach($mist_arr as $key=>$value){
if($value!=""){

if(in_array($key,$cookie_arr))
	cookie($key,$value);

$con[$key]=array("like","%".$value."%");
}
}
}


/*-----------以上是模糊查询字段------------*/

//精确数组
if($accute_arr){
foreach($accute_arr as $key=>$value){
if($value!=""){

if(in_array($key,$cookie_arr))
   cookie($key,$value);

   $con[$key]=$value;
}
}
}


/*-----------以上是精确查询字段------------*/

return $con;

}

//获得实体信息
function getObjectInfo($id,$object,$field=""){
$oop=M($object);
$list=$oop->find($id);
if($field=="")
	return $list;
else
	return $list[$field];

}

function getObjectList($object,$arr="1",$sort="",$num=""){
$oop=D($object);
$list=$oop->where($arr)->order($sort)->limit($num)->select();
return $list;
}


//剪裁字符串
function cut_str($sourcestr,$cutlength){
   $returnstr='';
   $i=0;
   $n=0;
   $str_length=strlen($sourcestr);//字符串的字节数
   while (($n<$cutlength) and ($i<=$str_length))
   {
      $temp_str=substr($sourcestr,$i,1);
      $ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码
      if ($ascnum>=224)    //如果ASCII位高与224，
      {
$returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范，将3个连续的字符计为单个字符        
         $i=$i+3;            //实际Byte计为3
         $n++;            //字串长度计1
      }
      elseif ($ascnum>=192) //如果ASCII位高与192，
      {
         $returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范，将2个连续的字符计为单个字符
         $i=$i+2;            //实际Byte计为2
         $n++;            //字串长度计1
      }
      elseif ($ascnum>=65 && $ascnum<=90) //如果是大写字母，
      {
         $returnstr=$returnstr.substr($sourcestr,$i,1);
         $i=$i+1;            //实际的Byte数仍计1个
         $n++;            //但考虑整体美观，大写字母计成一个高位字符
      }
      else                //其他情况下，包括小写字母和半角标点符号，
      {
         $returnstr=$returnstr.substr($sourcestr,$i,1);
         $i=$i+1;            //实际的Byte数计1个
         $n=$n+0.5;        //小写字母和半角标点等与半个高位字符宽...
      }
   }
         if ($str_length>$i){
          $returnstr = $returnstr . "...";//超过长度时在尾处加上省略号
      }
    return $returnstr;
}

//分页方法
function pageInfo($oop,$sort="",$con="1=1",$num=35){
//require_once("./Application/Common/Common/Mypage.class.php");// 导入分页类
 $count      = $oop->where($con)->count();// 查询满足要求的总记录数
 $Page       = new \Common\Common\Mypage($count,$num);// 实例化分页类 传入总记录数和每页显示的记录数 
 $data['show']       = $Page->show();// 分页显示输出 // 进行分页数据查询 注意limit方法的参数要使用Page类的属性 
 $data['list'] = $oop->where($con)->order($sort)->limit($Page->first_row.','.$Page->list_rows)->select();
 //echo $oop->GetLastSql();
 $data['total_num']=$Page->total_num;//总记录数
 $data['page_num']=$Page->page_num;//总分页
 return $data;
} 

function displayIcon($status=1){

if($status)
	return "<font color=green><b>√</b></font>";
else
	return "<font color=red><b>×</b></font>";
}

function getAreaInfo($parent_id,$json=true){
$oop=M("areas");
$con['parent_id']=$parent_id;
$list=$oop->where($con)->select();
$arr[0]['id']="";
$arr[0]['text']="--Please select--";


foreach($list as $key=>$value){
$arr[$key+1]['id']=$value['area_id'];
$arr[$key+1]['text']=$value['area_name'];
}

if($json)
	return json_encode($arr);
else
	return $list;
}

//验证身份证
function is_idcard( $id ){
    $id = strtoupper($id);
    $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
    $arr_split = array();
    if(!preg_match($regx, $id))
    {
        return FALSE;
    }
    if(15==strlen($id)) //检查15位
    {
        $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
 
        @preg_match($regx, $id, $arr_split);
        //检查生日日期是否正确
        $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
        if(!strtotime($dtm_birth))
        {
            return FALSE;
        } else {
            return TRUE;
        }
    }
    else           //检查18位
    {
        $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
        @preg_match($regx, $id, $arr_split);
        $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
        if(!strtotime($dtm_birth))  //检查生日日期是否正确
        {
            return FALSE;
        }
        else
        {
            //检验18位身份证的校验码是否正确。
            //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
            $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            $sign = 0;
            for ( $i = 0; $i < 17; $i++ )
            {
                $b = (int) $id{$i};
                $w = $arr_int[$i];
                $sign += $b * $w;
            }
            $n  = $sign % 11;
            $val_num = $arr_ch[$n];
            if ($val_num != substr($id,17, 1))
            {
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }
    }
 
}

function getInfoByID($id,$field=""){ 
//过了这年的生日才算多了1周岁 
        $date=strtotime(substr($id,6,8));
//获得出生年月日的时间戳 
        $today=strtotime('today');
//获得今日的时间戳 
        $diff=floor(($today-$date)/86400/365);
//得到两个日期相差的大体年数 
//strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比  

$birthday= strlen(id)==15 ? ('19' . substr($id, 6, 6)) : substr($id, 6, 8);
$data['birthday']=substr($birthday,0,4)."-".substr($birthday,4,2)."-".substr($birthday,6,8);
$data['age']=$data['birthday']>$today?($diff+1):$diff;
$data['sex'] = substr($id, (strlen($id)==15 ? -2 : -1), 1) % 2 ? 1 : 0;	
	
if($field)
	return $data[$field];		
else
	return $data;
} 


 /**
 +----------------------------------------------------------
 * 功能：计算文件大小
 +----------------------------------------------------------
 * @param int $bytes
 +----------------------------------------------------------
 * @return string 转换后的字符串
 +----------------------------------------------------------
 */
 function byteFormat($bytes) {
    $sizetext = array(" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
    return round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), 2) . $sizetext[$i];
 }
 
 // 无需递归生成树
 // 数组格式
 // $items = array(
 //     1 => array('id' => 1, 'pid' => 0, 'name' => '安徽省'),
 //     2 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'),
 //     3 => array('id' => 3, 'pid' => 1, 'name' => '合肥市'),
 //     4 => array('id' => 4, 'pid' => 3, 'name' => '长丰县'),
 //     5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'),
 // );	 
 function generateTree($items){
     $tree = array();
     foreach($items as $item){
         if(isset($items[$item['pid']])){
             $items[$item['pid']]['children'][] = &$items[$item['id']];
         }else{
             $tree[] = &$items[$item['id']];
         }
     }
     return $tree;
 }
 

?>